package com.caiyi.financial.nirvana.sms.mapper;

import com.caiyi.financial.nirvana.sms.base.mapper.BaseMapper;
import com.caiyi.financial.nirvana.sms.entity.SmsGatewayRecharge;
import com.caiyi.financial.nirvana.sms.querybean.GatewayRechargeQueryBean;
import com.caiyi.financial.nirvana.sms.vo.SmsGatewayRechargeVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface SmsGatewayRechargeMapper extends BaseMapper<SmsGatewayRecharge> {

    @SelectProvider(type = GatewayRechargeSqlProvider.class, method = "selectGatewayRechargeListByParams")
    List<SmsGatewayRechargeVo> selectGatewayRechargeListByParams(GatewayRechargeQueryBean bean);

    class GatewayRechargeSqlProvider {
        public String selectGatewayRechargeListByParams(GatewayRechargeQueryBean bean) {
            return new SQL() {
                {
                    SELECT("s.id,g.name,s.sms_num as smsNum,s.sms_money as smsMoney,s.recharge_time as rechargeTime,s.create_time as createTime,s.update_time as updateTime");
                    FROM("sms_gateway_recharge s");
                    LEFT_OUTER_JOIN("sms_gateway g on s.gateway_id=g.id");
                    if (!StringUtils.isEmpty(bean.getName())) {
                        WHERE("g.name LIKE concat('%',#{name},'%')");
                    }
                    if (bean.getStartTime() != null) {
                        WHERE("s.recharge_time >= #{startTime}");
                    }
                    if (bean.getEndTime() != null) {
                        WHERE("s.recharge_time <=date_add(#{endTime}, interval 1 day)");
                    }
                    ORDER_BY("id desc");
                }
            }.toString();
        }
    }

}
